using System.Linq;
using Shop.Backend.Api.Entity;
using Shop.Backend.Api.Params;
using Shop.Backend.Api.Repository;
using Shop.Backend.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Shop.Backend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    //这里的控制器是 Category
    public class CategoryController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Users> _usersRepository;
        private IRepository<Goods> _goodsRepository;
        private IRepository<GoodsCategory1> _goodsCategory1Repository;
        private IRepository<GoodsCategory2> _goodsCategory2Repository;
        private IRepository<GoodsCategory3> _goodsCategory3Repository;

        private TokenParameter _tokenParameter;


        public CategoryController(IConfiguration configuration
        , IRepository<Users> usersRepository
        , IRepository<Goods> goodsRepository
        , IRepository<GoodsCategory1> goodsCategory1Repository
        , IRepository<GoodsCategory2> goodsCategory2Repository
        , IRepository<GoodsCategory3> goodsCategory3Repository)
        {
            _configuration = configuration;
            _usersRepository = usersRepository;
            _goodsRepository = goodsRepository;
            _goodsCategory1Repository = goodsCategory1Repository;
            _goodsCategory2Repository = goodsCategory2Repository;
            _goodsCategory3Repository = goodsCategory3Repository;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }


        [HttpGet]
        public dynamic Get([FromQuery] QueryWithPager query)
        {


            // get请求默认从url中获取参数，如果需要使用实体接收参数，需要FromQuery特性
            var pageIndex = query.Pager.PageIndex;
            var pageSize = query.Pager.PageSize;

            var keyword = string.IsNullOrEmpty(query.Keyword) ? "" : query.Keyword.Trim();

            //以最小的一个类开始 组合出来我想要的完整分类

            var category3 = _goodsCategory3Repository.Table.ToList().Select(x =>
            {
                var category2 = _goodsCategory2Repository.Table.ToList()
                .Where(y => y.Id == x.FromCategory2Id)
                .Select(z =>
                {
                    var category1 = _goodsCategory1Repository.Table.ToList()
                    .Where(j => j.Id == z.FromCategory1Id).FirstOrDefault();

                    return new
                    {
                        Id = x.Id,
                        Category1 = category1.CategoryName,
                        Category2 = z.CategoryName,
                        Category3 = x.CategoryName,
                        CreatedTime = x.CreatedTime
                    };

                });

                return new
                {
                    Category = category2
                };

            });
            // var dogs = category3.SelectMany(p => p.category2[0].);



            // 如果keyword不为空，则再去查询一二三级分类   //还未测试
            // if (!string.IsNullOrEmpty(keyword))
            // {
            //     category3 = _goodsCategory3Repository.Table.ToList().Select(x =>
            //     {
            //         var category2 = _goodsCategory2Repository.Table.ToList()
            //         .Where(y => y.Id == x.FromCategory2Id).Select(z =>
            //         {
            //             var category1 = _goodsCategory1Repository.Table.ToList()
            //             .Where(k => k.CategoryName.Contains(keyword) || k.Id.ToString() == keyword)
            //             .Where(j => j.Id == z.FromCategory1Id)
            //             .FirstOrDefault();

            //             return new
            //             {
            //                 Id = x.Id,
            //                 Category1 = category1.CategoryName,
            //                 Category2 = z.CategoryName,
            //                 Category3 = x.CategoryName
            //             };

            //         });

            //         return new
            //         {
            //             Category = category2
            //         };

            //     });
            // }
            return JsonHelper.Serialize(new
            {
                Code = 1000,
                Category = category3,
                Data = new
                {
                    Pager = new
                    {
                        pageIndex,
                        pageSize,
                        rowsTotal = category3.Count(),
                    }
                },
                Msg = "获取分类列表成功^_^"
            });
        }

        [HttpPut]
        public dynamic GetCategory2(CreateCategory createCategory)
        {
            var category1Name = createCategory.FromGoodsCategory1Name.Trim();
            var category1Id = _goodsCategory1Repository.Table.ToList().Where(x => x.CategoryName == category1Name).FirstOrDefault();//获取到一级分类的ID
            var category2Name = _goodsCategory2Repository.Table.ToList().Where(x => x.FromCategory1Id == category1Id.Id);
            return new
            {
                Code = 1000,
                Data = category2Name,
                Msg = "获取二级分类成功" 
            };

        }
        [HttpPut,Route("category3")]
        public dynamic GetCategory3(CreateCategory createCategory)
        {
            var category2Name = createCategory.FromGoodsCategory2Name.Trim();
            var category2Id = _goodsCategory2Repository.Table.ToList().Where(x => x.CategoryName == category2Name).FirstOrDefault();//获取到二级分类的ID
            var category3Name = _goodsCategory3Repository.Table.ToList().Where(x => x.FromCategory2Id == category2Id.Id);
            return new
            {
                Code = 1000,
                Data = category3Name,
                Msg = "获取三级分类成功" 
            };

        }
        [HttpPut,Route("category1")]
        public dynamic GetCategory1()
        {
            var category1 = _goodsCategory1Repository.Table.ToList();
            return new
            {
                Code = 1000,
                Data = category1,
                Msg = "获取一级分类成功" 
            };

        }

        [HttpPost]
        public dynamic Post(CreateCategory createCategory)
        {
            var category1Name = createCategory.Category1Name.Trim();
            var category2Name = createCategory.Category2Name.Trim();
            var category3Name = createCategory.Category3Name.Trim();

            if (string.IsNullOrEmpty(category1Name) && string.IsNullOrEmpty(category2Name) && string.IsNullOrEmpty(category3Name))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "商品分类名不能为空，请检查一二三级分类是否正确。"
                };
            }
            //先去查一下数据库有没有这个分类 有的话维持有的现状

            //添加一级分类

            var isCategory1 = _goodsCategory1Repository.Table.Where(x => x.CategoryName == category1Name);

            var category1 = new GoodsCategory1
            {
                CategoryName = createCategory.Category1Name,
            };

            if (isCategory1.Count() < 1)
            {
                _goodsCategory1Repository.Insert(category1);
            }
            else
            {
                category1.Id = isCategory1.FirstOrDefault().Id;
            }

            //添加二级分类

            var isCategory2 = _goodsCategory2Repository.Table.Where(x => x.CategoryName == category2Name);

            var category2 = new GoodsCategory2
            {
                FromCategory1Id = category1.Id,
                CategoryName = createCategory.Category2Name,

            };
            if (isCategory2.Count() < 1)
            {
                _goodsCategory2Repository.Insert(category2);
            }
            else
            {
                category2.Id = isCategory2.FirstOrDefault().Id;
            }


            //添加三级分类

            var isCategory3 = _goodsCategory3Repository.Table.Where(x => x.CategoryName == category3Name && x.FromCategory2Id == category2.Id);

            var category3 = new GoodsCategory3
            {
                FromCategory2Id = category2.Id,
                CategoryName = createCategory.Category3Name,

            };

            if (isCategory3.Count() < 1 && isCategory2.Count() > 0)
            {
                _goodsCategory3Repository.Insert(category3);
            }



            return new
            {
                Code = 1000,
                // isCategory1 = isCategory1,
                Data = new
                {
                    Category1 = category1,
                    Category2 = category2,
                    Category3 = category3
                },
                Msg = "添加分类成功^_^"
            };

        }

    }
}